<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
    <title></title>
    <script src="js/mui.min.js"></script>
    <link href="css/mui.min.css" rel="stylesheet"/>
	<script>
		// if()
	</script>
</head>
<body>
	<header class="mui-bar mui-bar-nav">
		<a class="mui-icon mui-action-menu mui-icon-bars mui-pull-left" onclick="open_set()"></a>
		<h1 class="mui-title">主页</h1>
		<a class="mui-icon mui-action-menu mui-icon-reload mui-pull-right" onclick="new_shua()">刷新</a>
	</header>
	<div class="mui-content" id="works">
	
	</div>
	
	<script type="text/javascript" charset="utf-8">
		var works =document.getElementById("works");
	  	mui.init();
		mui.plusReady(function () {
			//如果第一次打开，则设置默认服务器
			new_shua()
			console.log(plus.storage.getItem("one_time"));
			if(plus.storage.getItem("one_time") == null){
					plus.storage.setItem("rw_server","http://rw.qingfuxiyu.us.kg/");
					plus.storage.setItem("yy_server","http://api.qingfuxiyu.us.kg/");
					plus.storage.setItem("class_id","81513578")
					plus.storage.setItem("one_time","非第一次")
					console.log("set server");
					// console.log(plus.storage.getItem("one_time"));
			}else{
				console.log("server is set");
			}
			
		 
			
		})
		document.addEventListener("plusready", function() {
			// 页面加载完成后自动执行
			
		});
		var runing = 0;
		
		//打开set也页面
		function open_set(id){
			//防止重复点击
			if(runing == 0){
				runing = 1;
				mui.openWindow({
					url: "set.html",
					id: String(id),
					extras: {
						//自定义扩展参数，可以用来处理页面间传值
						mubiao: "start"
					},
				})
				runing = 0;
			}
			else{
				return;
			}
		}
		
		
		//计时器
		function timedown(duration, callback) {
		    let timer = duration, minutes, seconds;
		    const interval = setInterval(function () {
		        minutes = parseInt(timer / 60, 10);
		        seconds = parseInt(timer % 60, 10);
		
		        minutes = minutes < 10 ? "0" + minutes : minutes;
		        seconds = seconds < 10 ? "0" + seconds : seconds;
		
		        //console.log(minutes + ":" + seconds); // 可以将这里改为将时间显示在页面上
		
		        if (--timer < 0) {
		            clearInterval(interval);
		            callback(); // 时间到，执行回调函数
		        }
		    }, 1000);
		}
		
		
		function open_mubiao(id){
			//防止重复点击
			console.log("启动");
			if(runing == 0){
				runing = 1;
				plus.storage.setItem("mubiao_id",id);
				plus.storage.setItem("mubiao","kai")
				mui.openWindow({
					url:"mubiao.html",
					id:id,
				})
				runing = 0;
			}
			else{
				console.log("重复点击");
				return;
			}
		}
		//open_mubiao()
		//刷新
		function new_shua(){
			var rw_server = plus.storage.getItem("rw_server");
			var class_id = plus.storage.getItem("class_id");
			works.innerHTML = "";
			console.log(class_id);
			//获取作业
			fetch(rw_server+"homework/"+class_id,{
			  method: 'get',
			})
			.then(response => response.json())
			.then(data => {
				console.log(JSON.stringify(data));
			
				// console.log(data);
				for (const key in data) {
					if (data.hasOwnProperty(key)) {
						//console.log(key);
						works.innerHTML += `
						<div class="mui-card">
							<!--页眉，放置标题-->
							<div class="mui-card-header">${data[key]["ye_mie"]}</div>
							<!--内容区-->
							<div class="mui-card-content">${data[key]["ye_zhong"]}</div>
							<!--页脚，放置补充信息或支持的操作-->
							<div class="mui-card-footer">
								<button type="button" class="mui-btn mui-btn-primary" id="${key}" onclick="open_mubiao(this.id)">开始</button>
							</div>
						</div>
						`
						
					}
				}
			})
			. catch(error => console.error('Error:', error))
		}
		
	</script>
	
</body>
</html>